<?php
namespace Purse\Controller;
use Purse\Controller\PurseController;

/**
 * SOS(众筹)模块
 */
class CrowdfundController extends PurseController {
	protected $_crowdfund;
	protected $_crowdfund_detail;
	
	protected function _initialize(){
		parent::_initialize();
		$this->_auth();
		
		$this->_moduleid = 111;
		$this->_crowdfund = new \Purse\Model\CrowdfundModel();
		$this->_crowdfund_detail = new \Purse\Model\CrowdfundDetailModel();
	}
    
    
	/**
	 * 发起众筹
	 */
	public function createCrowdfundAction(){
		$chatid = (int)I('request.chatid');
		$amount = I('request.amount');
		$memo = I('request.memo');
		$this->_createCrowdfund($chatid ,$amount,$memo);
	}
	
	private function _createCrowdfund($chatid ,$amount,$memo){
		if(empty($chatid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$amount = number_format($amount, 2, '.', '');
		
		if((bccomp($amount, '0.01', 2) < 0)){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
		}
		
		if((bccomp($amount, '10000.00', 2) > 0)){
			$this->status = 1001;
			$this->msg = "雪中送炭金额不能大于10000.00元.";
			$this->outPut();
		}
		
		
		$data = array();
		$data['amount'] = $amount;
		$data['custid'] = $this->_custInfo['custid'];
		$data['memo'] = $memo;
		$data['chatid'] = $chatid;
		$fundid = $this->_crowdfund->addCrowdfund($data);
		if($fundid){
			$arr['fundid'] = $fundid;
			$arr['amount'] = $amount;
			$arr['custid'] = $data['custid'];
			$arr['memo'] = $data['memo'];
			$arr['chatid'] = $chatid;
			$this->data = $arr;
			$this->outPut();
		}else{
			$this->status = 2500;
			$this->msg = L('STATUS_2500');
			$this->outPut();
		}
	}
	
	
	
	/**
	 * 转账
	 */
	public function sosTransferAction(){
		$fundid = (int)I('request.fundid');
		$amount = I('request.amount');
		$paypassword = I('request.paypassword');
		$memo = I('request.memo');
		$this->_sosTransfer($fundid , $amount , $paypassword , $memo);
	}
	
	private function _sosTransfer($fundid , $amount , $paypassword='' , $memo=''){
		if(empty($fundid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		if(bccomp($amount, '0.01', 2) < 0){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$crowdfundInfo = $this->_crowdfund->getCrowdfundById($fundid);
		if($crowdfundInfo['sosstatus'] == "1"){
			$this->status = 2608;
			$this->msg = L('STATUS_2608');
			$this->outPut();
		}
		
		$ret = $this->pay($crowdfundInfo['custid'] , $amount , 0 , $paypassword ,array('转账转出','转账转入') , false);
		
		if(!empty($ret[0]) && !empty($ret[1])){
			//众筹详情记录
			$fundData['fundid'] = $fundid;
			$fundData['custid'] = $this->_custInfo['custid'];
			$fundData['amount'] = $amount;
			$ret3 = $this->_crowdfund_detail->addCrowdfundDetail($fundData);
	
			if($ret3){
				
				$ret = $this->_crowdfund_detail->getTotal($fundid);
				
				$data = array();
				$data['fundid'] = $fundid;
				$data['fundamount'] = $ret['fundamount'];
				if((float)$ret['fundamount'] >= (float)$crowdfundInfo['amount']){
					$data['sosstatus'] = 1;
				}
				$ret6 = $this->_crowdfund->updateCrowdfund($data);
				
				$custInfo = $this->_getCustInfo($crowdfundInfo['custid']);
					
				//发给群
				$msg['from_cust_name'] = $this->_custInfo['nickname'];
				$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
				$msg['type'] = 60;
				$msg['session_type'] = 2;
				$msg['session_id'] = (int)$crowdfundInfo['chatid'];
				$msg['fundid'] = $fundid;
				$msg['amount'] = $amount;
				//$msg['content'] = "{$this->_custInfo['nickname']}向{$custInfo['nickname']}支付{$amount}元 雪中送炭";
				$msg['content'] = "{0}向{1}支付{$amount}元 雪中送炭";
				$msg['content_param'][0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
				$msg['content_param'][1] = array('custid'=>$custInfo['custid'],'nickname'=>$custInfo['nickname']);
				
				\im::sendMsg(2, array(), array($crowdfundInfo['chatid']), $msg);
				
				$this->outPut();
			}else{
				$this->status = 2607;
				$this->msg = L('STATUS_2607');
				$this->outPut();
			}
		}else{
			$this->status = 2607;
			$this->msg = L('STATUS_2607');
			$this->outPut();
		}
	}
	

	/**
	 * 众筹详情
	 */
	public function crowdfundInfoAction(){
		$fundid = (int)I('request.fundid');
		$this->_crowdfundInfo($fundid);
	}
	
	
	private function _crowdfundInfo($fundid){
		if(empty($fundid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		$crowdfundInfo = $this->_crowdfund->getCrowdfundById($fundid);
		if(!empty($crowdfundInfo)){
			$custInfo = $this->_getCustInfo($crowdfundInfo['custid']);
			$crowdfundInfo['nickname'] = $custInfo['nickname'];
		}
		$crowdfundList = $this->_crowdfund_detail->getCrowdfundList($fundid);
		$crowdfundInfo['list'] = $crowdfundList;
		if(!empty($crowdfundInfo['list'])){
			$tmp = '0.00';
			$maxAmountIdx = 0;
			foreach($crowdfundInfo['list'] as $idx => &$item){
				$custInfo = $this->_getCustInfo($item['custid']);
				$item['nickname'] = $custInfo['nickname'];
				
				$item['ismax'] = 0;
				
				if($item['amount'] > $tmp) $maxAmountIdx = $idx;
				
				$tmp = $item['amount'];
			}
			
			$crowdfundInfo['list'][$maxAmountIdx]['ismax'] = 1;
			
			foreach ($crowdfundInfo['list'] as $idx => $row) {
				$tmptime[$idx]  = $row['createtime'];
			}
			array_multisort($tmptime, SORT_DESC, $crowdfundInfo['list']);
			
		}else{
			$crowdfundInfo['list'] = array();
		}
		
		$this->data = $crowdfundInfo;
		$this->outPut();
	}
	

}
